package com.zhiche.lisa.bms.controller.driver;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.pojo.vo.driver.PriceconfFreightVO;
import com.zhiche.lisa.bms.service.driver.IPriceconfFreightService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 线路单公里运价 前端控制器
 * </p>
 *
 * @author zhaoguixin
 * @since 2018-08-30
 */
@RestController
@RequestMapping("/route_freight")
@Slf4j
@Api(description = "线路单公里运价", tags = {"线路单公里运价"})
public class PriceconfFreightController {

    @Autowired
    private IPriceconfFreightService priceconfFreightService;


    @PostMapping(value = "/page_list")
    @ApiOperation(value = "分页获得线路单公里运价", notes = "分页获得线路单公里运价",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceconfFreightVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<PriceconfFreightVO>> listByPage(@RequestBody @ApiParam(name = "PriceconfFreightVO", value = "线路单公里运价VO", required = true) Page<PriceconfFreightVO> pageVo) {
        log.info("PriceconfFreightController.listByPage params : {}", pageVo);
        RestfulResponse<Page<PriceconfFreightVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(priceconfFreightService.listByPage(pageVo));
        } catch (BaseException e) {
            log.error("PriceconfFreightController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfFreightController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @GetMapping(value = "/getById")
    @ApiOperation(value = "根据ID得到线路单公里运价", notes = "根据ID得到线路单公里运价",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "id", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceconfFreightVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<PriceconfFreightVO> getById(Integer id) {
        log.info("PriceconfFreightController.getById param : {}", id);
        RestfulResponse<PriceconfFreightVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(priceconfFreightService.getById(id));
        } catch (BaseException e) {
            log.error("PriceconfFreightController.getById ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfFreightController.getById ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加", notes = "添加",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Integer.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Integer> add(@ApiParam(name = "PriceconfFreightVO", value = "线路单公里运价VO", required = true) @RequestBody @Valid PriceconfFreightVO form, BindingResult br) {
        log.info("PriceconfFreightController.add param: {}", form);
        RestfulResponse<Integer> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(priceconfFreightService.add(form, br));
        } catch (BaseException e) {
            log.error("PriceconfFreightController.add ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfFreightController.add ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/update")
    @ApiOperation(value = "修改", notes = "修改",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> update(@ApiParam(name = "PriceconfFreightVO", value = "线路单公里运价VO", required = true) @RequestBody @Valid PriceconfFreightVO form, BindingResult br) {
        log.info("ScPriceconfFreightController.update param : {}", form);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = priceconfFreightService.update(form, br);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("PriceconfFreightController.update ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfFreightController.update ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/del")
    @ApiOperation(value = "删除", notes = "删除线路单公里运价",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "Integer", name = "id", value = "线路单公里运价ID", required = true)})
    public RestfulResponse<Boolean> del(Integer id) {
        log.info("ScPriceconfFreightController.del param : {}", id);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = priceconfFreightService.del(id);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("PriceconfFreightController.del ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("PriceconfFreightController.del ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

}

